Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jcomte23/Python_vanilla/llms.txt
Use this file to discover all available pages before exploring further.
Introduction to Strings
Strings in Python are sequences of characters used to store and manipulate text. Python provides powerful tools for working with strings, including various formatting methods, slicing techniques, and built-in string methods.Getting String Input
Theinput() function always returns strings by default:
The
input() function always returns a string, even if the user enters numbers. To work with numeric input, you must explicitly convert it using int() or float().String Formatting Methods
Python offers multiple ways to format and display strings. All methods are valid - choose based on your preference and use case.1. Comma Separation
2. String Concatenation
3. format() Method
4. F-strings (Recommended)
String Slicing
String slicing allows you to extract portions of a string using index notation[start:end:step].
Slicing syntax:
string[start:end:step]start: Starting index (inclusive)end: Ending index (exclusive)step: Increment between characters
String Search Methods
The in Operator
Check if a substring exists within a string:
index() Method
Find the position of a substring:rindex() Method
Search from right to left:String Transformation Methods
Case Conversion
String Analysis Methods
String Validation Methods
startswith() and endswith()
isdigit()
String Modification Methods
replace()
Replace substrings:strip()
Remove leading and trailing whitespace:Related methods:
lstrip() removes left whitespace, rstrip() removes right whitespace.Common String Methods Summary
| Method | Description | Example |
|---|---|---|
len() | Get string length | len("hello") → 5 |
upper() | Convert to uppercase | "hello".upper() → “HELLO” |
lower() | Convert to lowercase | "HELLO".lower() → “hello” |
capitalize() | Capitalize first letter | "hello".capitalize() → “Hello” |
title() | Capitalize each word | "hello world".title() → “Hello World” |
count() | Count occurrences | "hello".count("l") → 2 |
replace() | Replace substring | "hello".replace("l", "r") → “herro” |
strip() | Remove whitespace | " hi ".strip() → “hi” |
startswith() | Check prefix | "hello".startswith("he") → True |
endswith() | Check suffix | "hello".endswith("lo") → True |
isdigit() | Check if numeric | "123".isdigit() → True |
index() | Find substring position | "hello".index("e") → 1 |